package com.example.administrator.android2019.Note;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.administrator.android2019.R;

import java.util.ArrayList;

public class NoteActivity extends AppCompatActivity implements AdapterView.OnItemClickListener,AdapterView.OnItemLongClickListener {
    ListView listView;
    ImageView image_add;

    NoteBean noteBean;
    NoteAdapter noteAdapter;
    NoteSqlLiteHelper helper;
    ArrayList<NoteBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note);
        listView=findViewById(R.id.note_listview);
        image_add=findViewById(R.id.note_image_add);
         NoteSqlLiteHelper helper;
        /* ListView 默认数据展示*/
        showData();

        /* 点击事件：跳转*/
        View.OnClickListener listener=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(NoteActivity.this,RecordActivity.class);
                /* 1、startActivity(intent)的跳转从A到B：
                 *  若B页面要回传数据，还得通过intent.putExtra()
                 *2、startActivityForResult：
                 *  若B页面修改了数据，要回传数据到A页面，
                 *  在A页面中重写onActivityResult方法，就会自动执行*/
                startActivityForResult(intent,1);
            }
        };
        /* 图片绑定事件*/
        image_add.setOnClickListener(listener);

    }

    /**
     * 重写，自动执行。记录页面做了修改之后，数据回传到首页：
     * ListView数据展示
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1 && resultCode==2){
            /* ListView 数据的展示 方法*/
            showData();
        }
    }

    /**
     * ListView数据展示:
     *  调用数据库工具类查询出数据list
     *  调用自定义适配器 做数据展示
     */
    private void showData() {
        if (list!=null){
            list.clear();
        }
        helper=new NoteSqlLiteHelper(NoteActivity.this);
        list=helper.query();
        noteAdapter=new NoteAdapter(NoteActivity.this,list);
        listView.setAdapter(noteAdapter);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        noteBean= (NoteBean) listView.getItemAtPosition(position);
        Intent intent=new Intent(NoteActivity.this,RecordActivity.class);
        intent.putExtra("id",noteBean.getId());
        intent.putExtra("content",noteBean.getContent());
        intent.putExtra("time",noteBean.getTime());
        startActivityForResult(intent,1);

    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
        /*列表项长按之后的操作，弹出对话框*/
        AlertDialog.Builder dialog=new AlertDialog.Builder(NoteActivity.this);
        dialog.setTitle("删除");
        dialog.setMessage("确定要删除此记录吗？");
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                noteBean =list.get(position);
                if (helper.deleteDate(noteBean.getId())){
                    list.remove(position);
                    noteAdapter.notifyDataSetChanged();
                    Toast.makeText(NoteActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
                }
            }
        });
        dialog.setNegativeButton("取消",null);

        dialog.show();
        return true;
    }

}
